package com.tsd.base.controller;

import com.tsd.base.entity.BaseExpressOrgRate;
import com.tsd.base.entity.BaseExpressOrgRateExt;
import com.tsd.base.service.BaseExpressOrgRateService;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysUserExt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseExpressOrgRate物流快递-运费费率 控制层
 *
 * @author Hillpool
 * @date 2021/04/15 11:17:56
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "基础资料-物流快递-运费费率", description = "物流快递-运费费率的API描述")
public class BaseExpressOrgRateController extends BaseController {

    @Resource
    private BaseExpressOrgRateService baseExpressOrgRateService;

    @ApiOperation(value = "根据id获取物流快递-运费费率", notes = "根据id获取物流快递-运费费率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "物流快递-运费费率id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getBaseExpressOrgRateById.action", method = RequestMethod.POST)
    public HttpResult<Object> getBaseExpressOrgRateById() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            BaseExpressOrgRate baseExpressOrgRate = baseExpressOrgRateService.getBaseExpressOrgRateById(id);
            hr.setData(baseExpressOrgRate);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存物流快递-运费费率", notes = "保存物流快递-运费费率，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "物流快递-运费费率", required = true, paramType = "body", dataType = "BaseExpressOrgRate")
    })
    @ResponseBody
    @RequestMapping(value = "/saveBaseExpressOrgRate.action", method = RequestMethod.POST)
    public HttpResult<Object> saveBaseExpressOrgRate() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            BaseExpressOrgRateExt t = super.getParams(request, BaseExpressOrgRateExt.class);
            baseExpressOrgRateService.saveBaseExpressOrgRate(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询物流快递-运费费率", notes = "分页查询物流快递-运费费率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findBaseExpressOrgRates.action", method = RequestMethod.POST)
    public HttpResult<Object> findBaseExpressOrgRates() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            PageBean page = new PageBean(map);
            List<BaseExpressOrgRateExt> list = baseExpressOrgRateService.findBaseExpressOrgRates(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
